package example

import (
	"database/sql"
	"fmt"
	"game/game-db/mysql/dao"
	"game/game-db/orm/metag"
)

type SplitAutoEntity struct {
	ExampleParent
	id     int64  `orm:"id=AUTO;name=id;comment=唯一ID"`
	name   string `orm:"comment=名字;length=255"`
	num    int64  `orm:"comment=学号"`
	has    bool   `orm:"comment=是否有"`
	time11 int64  `orm:"comment=时间"`
}

func (t *SplitAutoEntity) TableMetag() metag.SplitTableMetag {
	tableTag := metag.SplitTableMetag{
		TableMetag: metag.TableMetag{
			Name:    "split_auto_entity",
			Comment: "例子",
			Indexes: make([]*metag.IndexMetag, 0),
		},
		SplitWay:      metag.VALUE,
		SplitByColumn: "time11",
	}
	return tableTag
}

func (t *SplitAutoEntity) Id() int64 {
	return t.id
}
func (t *SplitAutoEntity) SetId(id int64) {
	t.id = id
}
func (t *SplitAutoEntity) SetName(name string) {
	t.name = name
}
func (t *SplitAutoEntity) SetNum(num int64) {
	t.num = num
}
func (t *SplitAutoEntity) SetHas(has bool) {
	t.has = has
}
func (t *SplitAutoEntity) SetSomeInfo(someinfo string) {
	t.someinfo = someinfo
}
func (t *SplitAutoEntity) SetTime(time11 int64) {
	t.time11 = time11
}

func TestSplitAutoEntity(sqlDB *sql.DB) {
	idao := dao.NewSplitBaseDao[*SplitAutoEntity](sqlDB, &SplitAutoEntity{})
	fmt.Println(idao.Sql(&SplitAutoEntity{}))

	var entity SplitAutoEntity
	entity.SetName("AAAA")
	entity.SetNum(9875451)
	entity.SetHas(true)
	entity.SetSomeInfo("skdjal4w5e4")
	entity.SetTime(111)
	idao.Insert(&entity)

	// 下面的update语句不会生效，因为原实体中的ID字段没有被赋值，导致数据库中找不到需要更新的数据
	entity.SetSomeInfo("PPPPPP")
	idao.Update(&entity)

	entity.SetTime(222)
	idao.Insert(&entity)

	fmt.Println(entity.Id())
}
